package demo.main;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.jasper.constant.ImageType;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRAbstractExporter;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.j2ee.servlets.ImageServlet;

public abstract class MutiReport {
	protected String resultResult;
	protected JasperReportBuilder reportBuilder;

	protected void setResult(JasperPrint print, HttpServletRequest request,
			HttpServletResponse response) {
		if ("export".equals(request.getParameter("opt"))) {
			String fType = request.getParameter("ftype");
			if (fType != null) {
				try {
					exportFile(fType, response);
				} catch (DRException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} else {
			exportJson(print, request);
		}
	}

	private void exportJson(JasperPrint print, HttpServletRequest request) {
		JRAbstractExporter exporter = null;
		exporter = new JRHtmlExporter();
		StringBuffer sb = new StringBuffer();
		exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,
				request.getContextPath() + "/servlets/image?timestamp="
						+ System.currentTimeMillis() + "&image=");
		exporter.setParameter(
				JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
				Boolean.TRUE);
		exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, "");
		exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, "");
		exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, sb);
		request.getSession().setAttribute(
				ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, print);
		exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
		try {
			exporter.exportReport();
			this.resultResult = sb.toString();
		} catch (JRException e) {
			e.printStackTrace();
		}
	}

	private boolean exportFile(String fileType, HttpServletResponse response)
			throws DRException, IOException {
		String filename = System.currentTimeMillis() + "." + fileType;
		response.setHeader("Content-Disposition", "attachment; filename="
				+ filename);
		if (fileType.equals("csv")) {
			response.setContentType("application/csv");
			reportBuilder.toCsv(response.getOutputStream());
		} else if (fileType.equals("pdf")) {
			OutputStream outputStream = response.getOutputStream();
			JRPdfExporter pdfExporter = new JRPdfExporter();
			response.setContentType("application/pdf");
			response.setCharacterEncoding("UTF-8");
			pdfExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
			pdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, filename);
			pdfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
			pdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, reportBuilder.toJasperPrint());
			try {
				pdfExporter.exportReport();
				outputStream.flush();
				outputStream.close();
			} catch (JRException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else if (fileType.equals("xls")) {
			response.setContentType("application/xls");
			reportBuilder.toXls(response.getOutputStream());
		} else if (fileType.equals("txt")) {
			response.setContentType("application/txt");
			reportBuilder.toText(response.getOutputStream());
		} else if (fileType.equals("jpg")) {
			response.setContentType("application/jpg");
			reportBuilder.toImage(response.getOutputStream(), ImageType.JPG);
		} else if (fileType.equals("xml")) {
			response.setContentType("application/xml");
			reportBuilder.toXml(response.getOutputStream());
		} else {
			// error file type
			return false;
		}
		resultResult = "success";
		return true;
	}

	protected abstract void build(HttpServletRequest request,
			HttpServletResponse response);
}